//根据id获取数据并初始化表单
function initFormData(url,data,form,afterCallback,beforeCallback){
    AsyncGet(url, data, function(value){
        data=value.data||value;
        if(beforeCallback!=null) beforeCallback(data);
        if(data!=null&&!Array.isArray(data)){
            for(var i in data){
                var key=i.substr(i.indexOf("_")+1);
                if($(form).find("[name='"+key+"']").is("input:text,input:password,input:hidden")){
                    $(form).find("[name='"+key+"']").val(data[i]);
                }
                if($(form).find("[name='"+key+"']").is("span")){
                    $(form).find("[name='"+key+"']").text(data[i]);
                }
                if($(form).find("[name='"+key+"']").is("select")){
                    if(data[i]!=null){
                        if($(form).find("[name='"+key+"']").parent().find("select").length!=1){
                            var value=data[i]+"";
                            var length=$(form).find("[name='"+key+"']").parent().find("select").length;
                            for(var j=0;j<length;j++){
                                var select=$(form).find("[name='"+key+"']").parent().find("select:eq("+j+")");
                                var string=$(select).find("option:last").val()+"";
                                $(select).val(value.substr(0,string.length)).change();
                            }
                        }else{
                            $(form).find("[name='"+key+"']").val(data[i]).change();
                        }
                    }
                }
                if($(form).find("[name='"+key+"']").is("textarea")){
                    $(form).find("[name='"+key+"']").val(data[i]);
                }
            }
        }
        if(afterCallback!=null) afterCallback(data);
    });
}


function getFormData(form){
    var data={};
    $(form).find("input[name],select[name],textarea[name]").each(function(index,value){
        data[$(this).attr("name")]=$(this).val();
    });
    return data;
}

function getFileFormData(form){
    var formData=new FormData();
    $(form).find("input[name],select[name],textarea[name]").each(function(index,value){
        if($(this).is("[type='file']")){
            formData.append($(this).attr("name"),this.files[0]);
        }else{
            if($(this).val()!=null){
                formData.append($(this).attr("name"),$(this).val());
            }
        }
    });
    return formData;
}

function clearFormData(form){
    $(form).find("input[name],select[name],textarea[name]").each(function(){
        $(this).val("");
        if($(this).is("select.select2")) $(this).empty();
    })
}